package com.rsk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.rsk.dao.SysCompanyTypesRepository;
import com.rsk.dao.extend.SysCompanyTypesExtend;
import com.rsk.dao.utils.SearchDataUtils;
import com.rsk.entity.SysCompanyTypes;
import com.rsk.service.SysCompanyTypesService;
import com.rsk.utils.algorithm.SnowflakeIdWorker;
import com.rsk.utils.tools.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SysCompanyTypesServiceImpl implements SysCompanyTypesService {

    @Autowired
    private SysCompanyTypesRepository sysCompanyTypesRepository;

    @Autowired
    SysCompanyTypesExtend sysCompanyTypesExtend;

    @Autowired
    private SearchDataUtils searchDataUtils;

    /**
     * 删除机构类型
     * @param id
     * @param operatorUser
     * @return
     */
    @Transactional
    @Override
    public int delSysCompanysType(Long id, Long operatorUser) {
        return sysCompanyTypesRepository.delSysCompanyTypes(id,operatorUser);
    }

    /**
     * 修改机构类型
     * @param id
     * @param typeName
     * @param remarks
     * @param operatorUser
     * @return
     */
    @Transactional
    @Override
    public int updateSysCompanysType(Long id, String typeName, String remarks, Long operatorUser) {
        return sysCompanyTypesRepository.updateSysCompanyTypes(id,typeName,remarks,operatorUser);
    }

    /**
     * 新增机构类型
     * @param id
     * @param typeName
     * @param typeCode
     * @param remarks
     * @param operatorUser
     * @return
     */
    @Transactional
    @Override
    public int saveSysCompanysType(Long id, String typeName, String typeCode, String remarks, Long operatorUser) {

        SysCompanyTypes companyTypes = new SysCompanyTypes();
        companyTypes.setId(SnowflakeIdWorker.generateId(true));
        companyTypes.setTypeName(typeName);
        companyTypes.setTypeCode(typeCode);
        companyTypes.setRemarks(remarks);
        companyTypes.setOperatorUser(operatorUser);
        sysCompanyTypesRepository.saveAndFlush(companyTypes);
        return 1;
    }

    /**
     * 查询机构类型
     * @param typeName
     * @param nowPage
     * @param pageSize
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public List<Map> selectSysComapnyType(String typeName, Integer nowPage, Integer pageSize) {
        Map<String, Object> map = sysCompanyTypesExtend.selectCourseCategoryByPage(typeName);
        String sql = map.get("sql").toString();
        Map<String,Object> paramMap = (Map<String, Object>) map.get("paramMap");
        List<Map> list  = searchDataUtils.search(sql, paramMap, Map.class);
        return  list;
    }


}
